import urllib3
import json;import re
import requests
from util.operation_cookie import OperationCookie
urllib3.disable_warnings()

class Delete:
    def __init__(self):
        self.ss = requests.session()
        self.cookies_value = OperationCookie().get_data()
        self.re_str = r'"orderCode":"(.+?)",'

    # 获取订单列表
    def get_list(self):
        url = "https://api.zxyjsc.com/flyapi/order/list/search"
        querystring = {
            "keyword":"","status":"0","pageOffset":"1","pageSize":"10","version":"1.0","terminal":"1"}
        headers = {
        'Accept-Encoding': "gzip, deflate",
        'Host': "api.zxyjsc.com",
        'Connection': "Keep-Alive",
        'User-Agent': "okhttp/3.8.0",
        }
        response = self.ss.get(url, headers=headers, params=querystring, verify=False, cookies=self.cookies_value)
        res_text = response.text
        list = re.findall(self.re_str, res_text)
        print('订单个数为：%s' % len(list))
        print(res_text)
        if '请先登录' in res_text:
            print(res_text)
        return list


    # for循环取消订单
    def cancel_order(self):
        list = self.get_list()
        cookies = self.cookies_value
        # url = "https://api.zxyjsc.com/flyapi/order/batch/del"
        url_cancel = 'https://api.zxyjsc.com/flyapi/order/cancel'
        querystring02 = {"version":"1.0","terminal":"1"}
        headers02 = {
            'Accept-Encoding': "gzip, deflate",
            # 'Content-Type': "application/json",
            'Content-Type': "application/x-www-form-urlencoded",
            'Host': "api.zxyjsc.com",
            'Connection': "Keep-Alive",
            'User-Agent': "okhttp/3.8.0",
            'cache-control': "no-cache",
            }
        for i_order in list:
            data = {}
            data['orderCode'] = i_order
            res = self.ss.post(url_cancel,headers=headers02, data=data,params=querystring02,verify=False,cookies=cookies)
            a01 = res.text
            print('取消订单的响应结果为：', a01)
            # time.sleep(0.2)
            if '订单号为空' in a01:
                return 0
            # elif '请在'and'分钟后取消订单！' in a01:
                # return 0
            elif '当前订单状态是“已关闭”' in a01:
                res = self.detele_order(i_order)
                if '无法删除' in res:
                    return 0

    # 删除订单
    def detele_order(self,i_order):
        # list = self.get_list()
        cookies = self.cookies_value
        url = "https://api.zxyjsc.com/flyapi/order/batch/del"
        querystring02 = {"version":"1.0","terminal":"1"}
        headers02 = {
            'Accept-Encoding': "gzip, deflate",
            'Content-Type': "application/json",
            # 'Content-Type': "application/x-www-form-urlencoded",
            'Host': "api.zxyjsc.com",
            'Connection': "Keep-Alive",
            'User-Agent': "okhttp/3.8.0",
            'cache-control': "no-cache",
            }
        data = []
        data.append(i_order)
        data = json.dumps(data)
        res = self.ss.post(url,headers=headers02, data=data,params=querystring02,verify=False,cookies=cookies)

        a01 = res.text;print(a01)
        return a01

if __name__ == '__main__':
    Delete().get_list()
    while True:
        D01 = Delete()
        if D01.cancel_order() == 0:
            break
        elif len(D01.get_list()) < 1:
            break

